package com.seeyon.apps.gkcwplugin.quartz;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.CronScheduleBuilder;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

import com.seeyon.apps.gkcwplugin.manager.SyncZDManager;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.supervise.manager.SuperviseManagerImpl;


/**
 * 定时同步字典数据
 * @Description: TODO(这里用一句话描述这个类的作用) 
 * @CopyRightInformation : XXX(公司名称)
 * @Prject: 
 * @ApplicationName: GKCW_Plugin
 * @Title: SysZdTask.java
 * @Package: com.seeyon.apps.gkcwplugin.quartz
 * @author: wtxx
 * @date: 2023年3月24日 上午9:56:37
 * @version: V1.0
 */
public class SysZdTask implements Job{
	private static final Log logger=LogFactory.getLog(SysZdTask.class);
	private SyncZDManager syncZDManager=(SyncZDManager) AppContext.getBean("syncZDManager");

	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		Date date=new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		logger.info("-------------同步字典数据任务，进入SysZdTask类execute方法,time:"+sf.format(date));
		try {
			logger.info("----------------调用系统登录接口");
			String sessionId=syncZDManager.systemInOut();
			logger.info("----------------sessionId:"+sessionId);
			if (sessionId!=null&&!"".equals(sessionId)&&!"null".equals(sessionId)) {
				logger.info("----------------1-开始同步往来单位字典数据");
				syncZDManager.syncContactUnit(sessionId,true);
				logger.info("----------------1-结束同步往来单位字典数据");
				logger.info("----------------2-开始同步核算部门字典数据");
				syncZDManager.syncDepartment(sessionId,true);
				logger.info("----------------2-结束同步核算部门字典数据");
				logger.info("----------------3-开始同步核算人员字典数据");
				syncZDManager.syncPersonnel(sessionId,true);
				logger.info("----------------3-结束同步核算人员字典数据");
				logger.info("----------------4-开始同步银行账户字典数据");
				syncZDManager.syncBankAccounts(sessionId,true);
				logger.info("----------------4-结束同步银行账户字典数据");
				logger.info("----------------5-开始同步核算科目字典数据");
				syncZDManager.syncSubject(sessionId,true);
				logger.info("----------------5-结束同步核算科目字典数据");
			}else {
				logger.info("------------------SyncZDController:登录接口返回sessionId为空");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	public static void initCurrentEvent() {
		try {
			StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory();
			Scheduler scheduler = stdSchedulerFactory.getScheduler();
			JobDetail jobDetail=JobBuilder.newJob(SysZdTask.class).withIdentity("job1", "group1").build();
			Trigger trigger = TriggerBuilder.newTrigger()
					.withIdentity("trigger1",  "triggerGroup1")
					.startNow()
					.withSchedule(
							CronScheduleBuilder.cronSchedule("0 0 0,12 * * ?")
					).build();//0点执行
			scheduler.scheduleJob(jobDetail, trigger);
			scheduler.start();
			Thread.sleep(10L * 1000L);
		} catch (Exception e) {
			logger.info("---------------------------同步字典数据定时任务异常！");
			e.printStackTrace();
		}
	}

}
